Program main
use type
use var
implicit none
integer ::  step, iout, trj_step, i 
real(dp) :: last_E(5) 
real (dp) :: clock_start,clock_finish,c_ini,c_fin
logical :: trjexist
character (len=8) :: fname='Energy', fname2='Q', fname3='lQ'
Character(len=10) :: namefile1, dir_name

call cpu_time(clock_start)
open(222,file='data.out')

call input()
iout = 200 + 1
write(namefile1,'(I10)')int(1)
if (restart == 0) then
   frame = 0
   OPEN ( iout , FILE = trim(adjustr(fname2))//trim(adjustl(namefile1)),form='UNFORMATTED', access='SEQUENTIAL')
else
   OPEN ( iout , FILE = trim(adjustr(fname2))//trim(adjustl(namefile1)),form='UNFORMATTED', access='SEQUENTIAL',status='old')
endif

write(namefile1,'(I10)')int(1)
iout = 300 + 1 
open(iout,FILE=trim(adjustr(fname))//trim(adjustl(namefile1)),STATUS='replace')
write(iout,*)"@ step LdG Elastic Energy"

step = 0
trj_E = 0.d0
call initial()

if (restart == 1) then
!   call rtraj2()
   call rtraj(trj_step) ! keep the old style
   step = trj_step
   Print *, "Simulation continue from step ", step
   trj_E = 0.d0
endif

if (number_NP > 0) then
   call position_NP()
   if (droplet) call make_np_droplet()
   if (channel) call make_np_channel()
   if (cylinder) call make_np_cylinder()
endif
! Calculate q6
do i = 1, tnodes
   q(6,i) = - q(1,i) - q(4,i)
enddo
call define_parameter()

!Gibzburg - Landau
CALL GL(step,last_E)
write(*,*) " last energy of GL cycle "
write(*,'(5(f15.6))')last_E

close(iout) ! close Energy file 
iout = 200 + 1
close(iout) ! close Q1 file 
write(namefile1,'(I10)')int(1)
frame = 0
OPEN ( iout , FILE = trim(adjustr(fname3))//trim(adjustl(namefile1)),form='UNFORMATTED', access='SEQUENTIAL')
call wtraj(1,step,step)  ! write lQ file
call qmga(1,1)

call cpu_time (clock_finish)
call timer(clock_finish,clock_start)

end program main
